package com.martin.dms.services.impl;

import java.util.Date;

import com.martin.dms.dataservices.DeliveryDataService;
import com.martin.dms.dataservices.ItemDataService;
import com.martin.dms.dataservices.SupplierDataService;
import com.martin.dms.factories.DeliveryFactory;
import com.martin.dms.model.Delivery;
import com.martin.dms.model.Item;
import com.martin.dms.model.Supplier;
import com.martin.dms.services.DeliveryService;

/**
 * Implementation of the <code>DeliveryService</code>.
 * 
 * @author mtsvetkov
 */
public class DeliveryServiceImpl implements DeliveryService {

    private DeliveryDataService deliveryDataService;
    private DeliveryFactory deliveryFactory;
    private SupplierDataService supplierDataService;
    private ItemDataService itemDataService;

    /**
     * Constructor.
     * 
     * @param deliveryDataService - data service for the delivery entity.
     * @param deliveryFactory - factory service for the delivery entity.
     * @param supplierDataService - data service for the supplier entity.
     * @param itemDataService - data service for the item entity.
     */
    public DeliveryServiceImpl(DeliveryDataService deliveryDataService, DeliveryFactory deliveryFactory,
            SupplierDataService supplierDataService, ItemDataService itemDataService) {
        this.deliveryDataService = deliveryDataService;
        this.deliveryFactory = deliveryFactory;
        this.supplierDataService = supplierDataService;
        this.itemDataService = itemDataService;
    }

    /**
     * {@inheritDoc}
     */
    public void makeDelivery(long supplierId, long itemId, int amount) {
        Supplier supplier = supplierDataService.get(supplierId);
        Item item = itemDataService.get(itemId);
        Delivery delivery = deliveryFactory.create(supplier, item, amount, new Date());
        delivery = deliveryDataService.create(delivery);
        itemDataService.addAmount(itemId, amount);
    }

}
